from flask import make_response, jsonify, request
from apps.api import api
from apps.gm.globalmanger import GlobalManager
from apps.utils.getinterfaceipinfo import GetInterFaceIpInfo

@api.route('/v1.0/viewtest/', methods=['GET', 'POST'])
def test():
    """
     上面 /v1.0/test/ 定义的url最后带上"/"：
     1、如果接收到的请求url没有带"/"，则会自动补上，同时响应视图函数
     2、如果/v1.0/test/这条路由的结尾没有带"/"，则接收到的请求里也不能以"/"结尾，否则无法响应
    """
    # 获取参数
    #if request.method == "POST":
    		 # 获取表单参数
    #    username = request.form.get("username")
    #    password = request.form.get("password")
    		 # 获取json参数
    #    data = request.get_json()
    #else:
    		 #获取get参数
    #    username = request.args.get("username")
    #    password = request.args.get("password")
    data = {'username': 'xxx', 'password': 'xxxx'}
    response = jsonify(code=200,
                       msg="success",
                       data=data)
    return response
    # 也可以使用 make_response 生成指定状态码的响应
    # return make_response(response, 200)

@api.route('/v1.0/dphostinfo/', methods=['GET', 'POST'])
def dphostinfo():
    response = jsonify(code=200,
                       msg="success",
                       data=GlobalManager().get_dic_from_type(1))
    return response

@api.route('/v1.0/jsncinfo/', methods=['GET', 'POST'])
def jsncinfo():
    response = jsonify(code=200,
                       msg="success",
                       data=GlobalManager().jsnc_list)
    return response

@api.route('/v1.0/jhncinfo/', methods=['GET', 'POST'])
def jhncinfo():
    response = jsonify(code=200,
                       msg="success",
                       data=GlobalManager().jhnc_list)
    return response

@api.route('/v1.0/ccncinfo/', methods=['GET', 'POST'])
def ccncinfo():
    response = jsonify(code=200,
                       msg="success",
                       data=GlobalManager().ccnc_list)
    return response

@api.route('/v1.0/notename/', methods=['GET', 'POST'])
def notename():
    response = jsonify(code=200,
                       msg="success",
                       data=GlobalManager().md_name_dic)
    return response

@api.route('/v1.0/getallncinfo/', methods=['GET', 'POST'])
def getallncinfo():
    # 当多个主机不通时，接口返回时间变长
    all_nc_info = {}
    hosts = GlobalManager().get_dic_from_type(1)
    for host in hosts.keys():
        print(host)
        ret = getncinfo(host)
        all_nc_info[host] = ret

    response = jsonify(code=200,
                       msg="success",
                       data=all_nc_info)
    return response

@api.route('/v1.0/getipncinfo/<ip>', methods=['GET', 'POST'])
def getipncinfo(ip):
    response = jsonify(code=200,
                       msg="success",
                       data=getncinfo(ip))
    return response

def getncinfo(ip):
    print("ip =", ip)
    ret_dic = {}
    host_info = ip
    # 通过ip获取当前账号信息
    # "data":["192.9.9.11","22","user","K9279Z00","K9279Z00","0"]
    if ip in GlobalManager().get_dic_from_type(1):
        host_info = GlobalManager().get_dic_from_type(1)[ip]
        gifi = GetInterFaceIpInfo(host_info[0], host_info[1], host_info[2], host_info[3])

        nc_names = []
        type = host_info[5]
        if type == "0":
            nc_names = GlobalManager().jsnc_list
        elif type == "1":
            nc_names = GlobalManager().jhnc_list
        elif type == "2":
            nc_names = GlobalManager().ccnc_list
        ret_dic = gifi.get_interface_list(nc_names)
    return ret_dic